﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Janus.BaseEntitys
{
    /// <summary>
    /// 复杂条件类
    /// </summary>
    public class Condition
    {
        public Condition() { }
        public Condition(string prepend, string open, string close, string expression, string conjunction)
        {
            this.Prepend = prepend;
            this.Open = open;
            this.Close = close;
            this.Expression = expression;
            this.Conjunction = conjunction;
        }

        /// <summary>
        /// 前缀 如:where 列表中只会取第一个
        /// </summary>
        public string Prepend { get; set; }

        /// <summary>
        /// 开始 如:(,and,or
        /// </summary>
        public string Open { get; set; }

        /// <summary>
        /// 结束 如:)
        /// </summary>
        public string Close { get; set; }

        /// <summary>
        ///  表达式 id=1
        /// </summary>
        public string Expression { get; set; }

        /// <summary>
        /// 连接逻辑符 And /Or 列表中最后一个不取
        /// </summary>
        public string Conjunction { get; set; }
        public string ToFirst()
        {
            return string.Format("{0} {1} {2} {3} {4}", this.Prepend, this.Open, this.Expression, this.Close, this.Conjunction);
        }
        public string ToMiddle()
        {
            return string.Format("{0} {1} {2} {3}", this.Open, this.Expression, this.Close, this.Conjunction);
        }
        public string ToLast()
        {
            return string.Format("{0} {1} {2}", this.Open, this.Expression, this.Close);
        }

    }

    public class Conditioner
    {
        List<Condition> conditions = new List<Condition>();

        public Condition this[int index]
        {
            get { return conditions[index]; }
        }

        public void Add(Condition model)
        {
            conditions.Add(model);
        }
        public void Add(string prepend, string open, string close, string expression, string conjunction)
        {
            Condition model = new Condition(prepend, open, close, expression, conjunction);
            this.Add(model);
        }
        public void Remove(Condition model)
        {
            conditions.Remove(model);
        }
        public void RemoveAt(int index)
        {
            conditions.RemoveAt(index);
        }

        public Condition First
        {
            get
            {
                var condition = conditions.FirstOrDefault();
                if (condition != null)
                {
                    conditions.Remove(condition);
                }
                else {
                    condition = new Condition();
                }
                return condition;
            }
        }

        public Condition Last
        {
            get
            {
                var condition = conditions.LastOrDefault();
                if (condition != null)
                {
                    conditions.Remove(condition);
                }
                else {
                    condition = new Condition();
                }
                return condition;
            }
        }
        public string ToWhere
        {
            get
            {
                string result = string.Empty;
                var first = First.ToFirst();
                var last = Last.ToLast();
                var middle = string.Empty;
                if (this.conditions != null && this.conditions.Count > 0)
                {
                    middle = string.Join("", this.conditions.Select(c => c.ToMiddle()));
                }
                result = first + middle + last;
                return result;
            }
        }
    }
}
